package com.xm.adv.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.TextView;

import com.xm.adv.R;

import butterknife.BindView;
import butterknife.ButterKnife;


public class VoiceDialog extends Dialog {

    private Context context;
    private ViewHolder viewHolder;
    private int maxVoice;
    private int currentVoice;
    private AudioManager mAudioManager;
    public interface OnVoiceChangeListener{
        void change(int voice);
    }

    OnVoiceChangeListener onVoiceChangeListener;

    public VoiceDialog(Context context,  OnVoiceChangeListener onVoiceChangeListener) {
        super(context, R.style.dialog);
        this.context = context;
        this.onVoiceChangeListener = onVoiceChangeListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        init();
    }

    public void init() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_voice, null);
        setContentView(view);
        viewHolder = new ViewHolder(view);

        mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        maxVoice = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        currentVoice = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

        viewHolder.seekBar.setMax(maxVoice);
        viewHolder.seekBar.setProgress(currentVoice);
        viewHolder.textCurrent.setText("当前音量：" + currentVoice);
        viewHolder.textMax.setText("最大音量：" + maxVoice);

        viewHolder.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                viewHolder.textCurrent.setText("当前音量：" + progress);
                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,progress,1);
                onVoiceChangeListener.change(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.7); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
    }


    @Override
    public boolean onGenericMotionEvent(MotionEvent event) {
        if (event.getButtonState() == 128) {
            dismiss();
        }
        return super.onGenericMotionEvent(event);
    }

    static
    class ViewHolder {
        @BindView(R.id.textCurrent)
        TextView textCurrent;
        @BindView(R.id.textMax)
        TextView textMax;
        @BindView(R.id.seekBar)
        SeekBar seekBar;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}